public class ThreadDemo_线程不安全示例 {
    private static int number = 0;
    static class Counter {
        // 循环次数
        private static int MAX_COUNT = 1000000;
        // ++ 方法
        public static void incr() {
            for (int i = 0; i < MAX_COUNT; i++) {
                number++;
            }
        }
        // -- 方法
        public static void decr() {
            for (int i = 0; i < MAX_COUNT; i++) {
                number--;
            }
        }
    }
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            Counter.incr();
        });
        t1.start();

        Thread t2 = new Thread(() -> {
            Counter.decr();
        });
        t2.start();

        // 等待线程执行完成
        t1.join();
        t2.join();
        System.out.println("最终的结果：" + number);
    }
}
